﻿@* Generator: Template
    GeneratePrettyNames : true *@
@functions{
    public NFinal.Compile.Template.App.Models.EntityModel Model { get; set; }
}
using System;
using System.Collections.Generic;
using System.Text;
using NFinal.Extension;

namespace @{@Model.project}.@{@Model.app}.Models.Entity.@{@Model.database}
{
    public struct @{@Model.table.name}:NFinal.IModel
    {
		@foreach(var field in Model.table.fields){
		if(field.description!=null){
		/// <summary>
        /// @field.description
        /// </summary>
		}
		if(field.allowNull && field.isValueType){
		@:public @{@field.csharpType}? @{@field.structFieldName};
		}else{
		@:public @{@field.csharpType} @{@field.structFieldName};
		}
		}
		#region Nullable
		private bool __is_null__;
        public bool __Is_Null__
        {
            get {
                return __is_null__;
            }
            set
            {
                 __is_null__=value;
            }
        }
        public static implicit operator @{@Model.table.name}(string __value__)
        {
            var __value2__ = new @{@Model.table.name}();
            __value2__.__Is_Null__ = __value__==null;
            return __value2__;
        }
        public static  bool operator true(@{@Model.table.name} __value__)
        {
            return !__value__.__is_null__;
        }
        public static bool operator false(@{@Model.table.name} __value__)
        {
            return !__value__.__is_null__;
        }
		#endregion
		#region 从get列表初始化实体类
		public static bool TryParse(NFinal.NameValueCollection get,out @{@Model.table.name} par)
		{
			par=new @{@Model.table.name}();
			try
			{
			@foreach(var field in Model.table.fields){
				if(field.csharpType=="System.Int64"){
					if(field.allowNull){
						@:par.@{@field.structFieldName}=null;
						@:if(get["@{@field.structFieldName}"]!=null)
						@:{
							@:par.@{@field.structFieldName}=get["@{@field.structFieldName}"].AsVar();
						@:}
					}else{
						@:System.Int64.TryParse(get["@{@field.structFieldName}"],out par.@{@field.structFieldName});
					}
				}else if(field.csharpType=="System.UInt64"){
					if(field.allowNull){
						@:par.@{@field.structFieldName}=null;
						@:if(get["@{@field.structFieldName}"]!=null)
						@:{
							@:par.@{@field.structFieldName}=get["@{@field.structFieldName}"].AsVar();
						@:}
					}else{
						@:System.UInt64.TryParse(get["@{@field.structFieldName}"],out par.@{@field.structFieldName});
					}	
				}else if(field.csharpType=="System.Int32"){
					if(field.allowNull){
						@:par.@{@field.structFieldName}=null;
						@:if(get["@{@field.structFieldName}"]!=null)
						@:{
							@:par.@{@field.structFieldName}=get["@{@field.structFieldName}"].AsVar();
						@:}
					}else{
						@:System.Int32.TryParse(get["@{@field.structFieldName}"],out par.@{@field.structFieldName});
					}	
				}else if(field.csharpType=="System.UInt32"){
					if(field.allowNull){
						@:par.@{@field.structFieldName}=null;
						@:if(get["@{@field.structFieldName}"]!=null)
						@:{
							@:par.@{@field.structFieldName}=get["@{@field.structFieldName}"].AsVar();
						@:}
					}else{
						@:System.UInt32.TryParse(get["@{@field.structFieldName}"],out par.@{@field.structFieldName});
					}	
				}else if(field.csharpType=="System.Int16"){
					if(field.allowNull){
						@:par.@{@field.structFieldName}=null;
						@:if(get["@{@field.structFieldName}"]!=null)
						@:{
							@:par.@{@field.structFieldName}=get["@{@field.structFieldName}"].AsVar();
						@:}
					}else{
						@:System.Int16.TryParse(get["@{@field.structFieldName}"],out par.@{@field.structFieldName});
					}	
				}else if(field.csharpType=="System.UInt16"){
					if(field.allowNull){
						@:par.@{@field.structFieldName}=null;
						@:if(get["@{@field.structFieldName}"]!=null)
						@:{
							@:par.@{@field.structFieldName}=get["@{@field.structFieldName}"].AsVar();
						@:}
					}else{
						@:System.UInt16.TryParse(get["@{@field.structFieldName}"],out par.@{@field.structFieldName});
					}	
				}else if(field.csharpType=="System.Byte"){
					if(field.allowNull){
						@:par.@{@field.structFieldName}=null;
						@:if(get["@{@field.structFieldName}"]!=null)
						@:{
							@:par.@{@field.structFieldName}=get["@{@field.structFieldName}"].AsVar();
						@:}
					}else{
						@:System.Byte.TryParse(get["@{@field.structFieldName}"],out par.@{@field.structFieldName});
					}	
				}else if(field.csharpType=="System.SByte"){
					if(field.allowNull){
						@:par.@{@field.structFieldName}=null;
						@:if(get["@{@field.structFieldName}"]!=null)
						@:{
							@:par.@{@field.structFieldName}=get["@{@field.structFieldName}"].AsVar();
						@:}
					}else{
						@:System.SByte.TryParse(get["@{@field.structFieldName}"],out par.@{@field.structFieldName});
					}	
				}else if(field.csharpType=="System.Single"){
					if(field.allowNull){
						@:par.@{@field.structFieldName}=null;
						@:if(get["@{@field.structFieldName}"]!=null)
						@:{
							@:par.@{@field.structFieldName}=get["@{@field.structFieldName}"].AsVar();
						@:}
					}else{
						@:System.Single.TryParse(get["@{@field.structFieldName}"],out par.@{@field.structFieldName});
					}
				}else if(field.csharpType=="System.Decimal"){
					if(field.allowNull){
						@:par.@{@field.structFieldName}=null;
						@:if(get["@{@field.structFieldName}"]!=null)
						@:{
							@:par.@{@field.structFieldName}=get["@{@field.structFieldName}"].AsVar();
						@:}
					}else{
						@:System.Decimal.TryParse(get["@{@field.structFieldName}"],out par.@{@field.structFieldName});
					}
				}else if(field.csharpType=="System.Double"){
					if(field.allowNull){
						@:par.@{@field.structFieldName}=null;
						@:if(get["@{@field.structFieldName}"]!=null)
						@:{
							@:par.@{@field.structFieldName}=get["@{@field.structFieldName}"].AsVar();
						@:}
					}else{
						@:System.Double.TryParse(get["@{@field.structFieldName}"],out par.@{@field.structFieldName});
					}
				}else if(field.csharpType=="System.String"){
						@:par.@{@field.structFieldName}=get["@{@field.structFieldName}"];
				}else if(field.csharpType=="System.Byte[]"){
                }else if(field.csharpType=="System.Boolean"){
                    if (field.allowNull) {
						@:par.@{@field.structFieldName}=null;
						@:if(get["@{@field.structFieldName}"]!=null)
						@:{
							@:par.@{@field.structFieldName}=get["@{@field.structFieldName}"].AsVar();
						@:}
					}else{
						@:System.Boolean.TryParse(get["@{@field.structFieldName}"],out par.@{@field.structFieldName});
					}
				}else if(field.csharpType=="System.DateTime"){
					if(field.allowNull){
						@:par.@{@field.structFieldName}=null;
						@:if(get["@{@field.structFieldName}"]!=null)
						@:{
							@:par.@{@field.structFieldName}=get["@{@field.structFieldName}"].AsVar();
						@:}
					}else{
						@:System.DateTime.TryParse(get["@{@field.structFieldName}"],out par.@{@field.structFieldName});
					}
				}else if(field.csharpType=="System.TimeSpan"){
					if(field.allowNull){
						@:par.@{@field.structFieldName}=null;
						@:if(get["@{@field.structFieldName}"]!=null)
						@:{
							@:par.@{@field.structFieldName}=get["@{@field.structFieldName}"].AsVar();
						@:}
					}else{
						@:System.TimeSpan.TryParse(get["@{@field.structFieldName}"],out par.@{@field.structFieldName});
					}
				}else if(field.csharpType=="System.DateTimeOffset"){
					if(field.allowNull){
						@:par.@{@field.structFieldName}=null;
						@:if(get["@{@field.structFieldName}"]!=null)
						@:{
							@:par.@{@field.structFieldName}=get["@{@field.structFieldName}"].AsVar();
						@:}
					}else{
						@:System.DateTimeOffset.TryParse(get["@{@field.structFieldName}"],out par.@{@field.structFieldName});
					}
				}else if(field.csharpType=="System.Object"){
						@:par.@{@field.structFieldName}=get["@{@field.structFieldName}"];
				}else if(field.csharpType=="System.Guid"){
					if(field.allowNull){
						@:par.@{@field.structFieldName}=null;
						@:if(get["@{@field.structFieldName}"]!=null)
						@:{
							@:par.@{@field.structFieldName}=System.Guid.Parse(get["@{@field.structFieldName}"]);
						@:}
					}else{
						@:System.Guid.TryParse(get["@{@field.structFieldName}"],out par.@{@field.structFieldName});
					}
				}else if(field.csharpType=="Microsoft.SqlServer.Types.SqlGeography"){
				}else if(field.csharpType=="Microsoft.SqlServer.Types.SqlGeometry"){
				}else if(field.csharpType=="Microsoft.SqlServer.Types.SqlHierarchyId"){
				}
			}
			}
			catch
			{
				return false;
			}
			return true;
		}
		#endregion
		#region 写Json字符串
		public void WriteJson(System.IO.TextWriter tw)
		{
			tw.Write("{");
			@{int i=0;
			foreach(var field in Model.table.fields){
				if(i!=0){
					@:tw.Write(",");
				}
				if(field.jsonType=="Number"){
					@:tw.Write("\"@{@field.structFieldName}\":");
					if(field.allowNull){
					@:tw.Write(@{@field.structFieldName}==null?"null" : @{@field.structFieldName}.ToString());
					}else{
					@:tw.Write(@{@field.structFieldName}.ToString());
					}
				}else if(field.jsonType=="Bool"){
					@:tw.Write("\"@{@field.structFieldName}\":");
					if(field.allowNull){
					@:tw.Write(@{@field.structFieldName}==null?"null":((bool)@{@field.structFieldName}?"true":"false"));
					}else{
					@:tw.Write(@{@field.structFieldName}?"true":"false");
					}
				}else if(field.jsonType=="Time"){
					@:tw.Write("\"@{@field.structFieldName}\":");
					@:tw.Write("\"");
					if(field.allowNull){
					@:tw.Write(@{@field.structFieldName}==null?"null":@{@field.structFieldName}.ToString());
					}else{
					@:tw.Write(@{@field.structFieldName}.ToString());
					}
					@:tw.Write("\"");
				}else if(field.jsonType=="String"){
					@:tw.Write("\"@{@field.structFieldName}\":");
					@:tw.Write("\"");
					if(field.allowNull){
					@:tw.Write(@{@field.structFieldName}==null?"null":@{@field.structFieldName});
					}else{
					@:tw.Write(@{@field.structFieldName});
					}
					@:tw.Write("\"");
				}else if(field.jsonType=="Object"){
					@:tw.Write("\"@{@field.structFieldName}\":");
					@:tw.Write("\"");
					if(field.allowNull){
					@:tw.Write(@{@field.structFieldName}==null?"null":@{@field.structFieldName}.ToString());
					}else{
					@:tw.Write(@{@field.structFieldName}.ToString());
					}
					@:tw.Write("\"");
				}else if(field.jsonType=="Base64"){
					@:tw.Write("\"@{@field.structFieldName}\":");
					@:tw.Write("\"");
					if(field.allowNull){
					@:tw.Write(@{@field.structFieldName}==null?"null":Convert.ToBase64String(@{@field.structFieldName}));
					}else{
					@:tw.Write(Convert.ToBase64String(@{@field.structFieldName}));
					}
					@:tw.Write("\"");
				}
			i++;
			}
			}
			tw.Write("}");
		}
		#endregion
    }
}
